package com.shuai.gateway.utils;

import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import org.apache.commons.lang3.StringUtils;

/**
 * 签名工具类
 */
public class AuthenticationSignUtils {

    /**
     * 使用SHA256算法生成签名。参数顺序：api-key，api-secret，timestamp，body
     */
    public static String generateSign(String apiKey, String apiSecret, long timestamp, String body) {
        if (StringUtils.isAnyBlank(apiKey, apiSecret)) {
            return "error-sign";
        }
        if (!TimestampUtils.inTimeRange(timestamp)) {
            return "error-sign";
        }
        String content = StringUtils.join(apiKey, apiSecret, timestamp, body, "#");
        Digester digester = new Digester(DigestAlgorithm.SHA256);
        return digester.digestHex(content);
    }
}
